package com.hitrust.gs.dao;

import java.util.ArrayList;

import com.hitrust.gs.bean.GSDictionaryBean;
import com.hitrust.gs.bean.GSOperatorBean;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.lib.db.GSBaseDAO;
import com.hitrust.gs.util.exception.GSException;

/**
 * 字典表
 * author : edward ma
 * create date 2007-01-16
 *
 */
public class GSDictionaryDAO extends GSBaseDAO 
{
    public boolean isExist(String operId) throws GSException 
    {
		String tSql = "select * from gs_dictionary where id = ?";
		boolean tFlag = false;
		Object[] tObj = { operId };
		try 
		{
			ArrayList tList = this.executeQuery(new GSDictionaryBean(), tSql,tObj);
			if (tList.size() > 0) 
			{
				tFlag = true;
			}
		} 
		catch (Exception e) 
		{
			GSLogWriter.error(this, e.toString());
			throw new GSException(GSException.E19999, GSException.E19999_MSG);
		}
		return tFlag;
	}
    
    //得唯一记录
    public GSDictionaryBean queryById(String id) throws GSException
    {
        String tSql = "select * from gs_dictionary where id = ? ";
        ArrayList tList = null;
        GSDictionaryBean tGSDictionaryBean = null;
        Object[] tObj = {id};
        try
        {
            tList = this.executeQuery(new GSDictionaryBean(),tSql,tObj);
            if (tList.size() == 1) 
            {
				tGSDictionaryBean = (GSDictionaryBean) tList.get(0);
			} 
            else 
            {
				throw new GSException(GSException.E10002,GSException.E10002_MSG);
			}
        }
        catch(Exception e)
        {
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tGSDictionaryBean;
    } 
    
    //某类别下的记录
    public ArrayList queryByParentId(String parentId) throws GSException
    {
        ArrayList tList = null;
        StringBuffer tSql = new StringBuffer("select * from gs_dictionary where parent_id = ? order by display_order");
        Object[] tObj = {parentId};

        GSLogWriter.debug(this,tSql.toString());
        try
        {
            tList = this.executeQuery(new GSDictionaryBean(),tSql.toString(),tObj);
        }
        catch(Exception e)
        {
            GSLogWriter.error(this,e.toString());
            throw new GSException(GSException.E19999,GSException.E19999_MSG);
        }
        return tList;
    }
}
